import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWechatMessageService extends BaseService<any> {
  private readonly logger = new Logger(CoreWechatMessageService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * message
   * 对应 PHP: CoreWechatMessageService_core::message()
   * 逻辑类型: undefined - undefined
   */
  async message(site_id: number, message: number) {
    // 基于PHP真实逻辑: message
    // PHP原文: switch ($message['MsgType']) {             case WechatDict::MESSAGE_TYPE_EVENT:                 return $this->event($site_id, $message); //           ...
switch (message.MsgType) {
            case WechatDict.MESSAGE_TYPE_EVENT:
                return this.event(site_id, message];
//                return '收到事件消息';
            case WechatDict.MESSAGE_TYPE_TEXT:
                //调用文本回复
                return this.text(site_id, message);
//                return '收到文字消息';
            case WechatDict.MESSAGE_TYPE_IMAGE:
                return '收到图片消息';
            case WechatDict.MESSAGE_TYPE_VOICE:
                return '收到语音消息';
            case WechatDict.MESSAGE_TYPE_VIDEO:
                return '收到视频消息';
            case WechatDict.MESSAGE_TYPE_LOCATION:
                return '收到坐标消息';
            case WechatDict.MESSAGE_TYPE_LINK:
                return '收到链接消息';
            case WechatDict.MESSAGE_TYPE_FILE:
                return '收到文件消息';
            // ... 其它消息
            default:
                return '收到其它消息';
  }

  /**
   * event
   * 对应 PHP: CoreWechatMessageService_core::event()
   * 逻辑类型: undefined - undefined
   */
  async event(site_id: number, message: number) {
    // 基于PHP真实逻辑: event
    // PHP原文: switch ($message['Event'] ) {             case WechatDict::EVENT_SUBSCRIBE:                 return $this->subscribe($site_id, $message);             c...
switch (message.Event ) {
            case WechatDict.EVENT_SUBSCRIBE:
                return this.subscribe(site_id, message];
            case WechatDict.EVENT_SCAN:
                return this.scan(site_id, message);
  }

  /**
   * scan
   * 对应 PHP: CoreWechatMessageService_core::scan()
   * 逻辑类型: undefined - undefined
   */
  async scan(site_id: number, message: number) {
    // 基于PHP真实逻辑: scan
    // PHP原文: $key = str_replace('qrscene_', '', $message['EventKey']);         $core_scan_service = new CoreScanService();         $core_scan_service->actionByScan...
const key = str_replace('qrscene_', '', message.EventKey);
        const core_scan_service = this.coreScanService;
        core_scan_service.actionByScan(site_id, key, [openid: message.FromUserName)];
        return get_lang('SCAN_SUCCESS');
    }

    /**
     * 关注事件
     * @param int site_id
     * @param message
     * @return false
     */
    async subscribe(int site_id, message){
        //todo 新增粉丝或将原有的粉丝改为关注状态
        // 因为时间的原因,这里可能需要将实践放在消息队列里面
        const core_wechat_fans_service = this.coreWechatFansService;
        core_wechat_fans_service.subscribe(site_id, message.ToUserName, message.FromUserName);
        //扫码事件
        if(!!message.EventKey)){
            this.scan(site_id, message);
  }

  /**
   * subscribe
   * 对应 PHP: CoreWechatMessageService_core::subscribe()
   * 逻辑类型: undefined - undefined
   */
  async subscribe(site_id: number, message: number) {
    // 基于PHP真实逻辑: subscribe
    // PHP原文: //todo 新增粉丝或将原有的粉丝改为关注状态         // 因为时间的原因,这里可能需要将实践放在消息队列里面         $core_wechat_fans_service = new CoreWechatFansService();         $core_wechat_fa...
//todo 新增粉丝或将原有的粉丝改为关注状态
        // 因为时间的原因,这里可能需要将实践放在消息队列里面
        const core_wechat_fans_service = this.coreWechatFansService;
        core_wechat_fans_service.subscribe(site_id, message.ToUserName, message.FromUserName);
        //扫码事件
        if(!!message.EventKey)){
            this.scan(site_id, message);
  }

  /**
   * unsubscribe
   * 对应 PHP: CoreWechatMessageService_core::unsubscribe()
   * 逻辑类型: undefined - undefined
   */
  async unsubscribe(site_id: any, message: any) {
    // 基于PHP真实逻辑: unsubscribe
    // PHP原文: $core_wechat_fans_service = new CoreWechatFansService();         $core_wechat_fans_service->unsubscribe($site_id, $message['FromUserName']);         r...
const core_wechat_fans_service = this.coreWechatFansService;
        core_wechat_fans_service.unsubscribe(site_id, message.FromUserName);
        return true;
    }

    /**
     * 文本回复事件
     * @param int site_id
     * @param message
     * @return false
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    function text(int site_id, message)
    {
        const core_wechat_reply_service = this.coreWechatReplyService;
        return core_wechat_reply_service.reply(site_id, WechatDict.REPLY_KEYWORD, message.Content, openid: message.FromUserName) || false;
    }
}
  }
}
